//
//  PageView.m
//  ScrollViewDemo
//
//  Created by SmartStrategy on 2019/4/8.
//  Copyright © 2019 SmartStrategy. All rights reserved.
//

#import "PageView.h"

@interface PageView ()<UIScrollViewDelegate>
{
    NSArray *_titlesArray;
    NSArray *_controllersArray;
}


@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UIScrollView *scrollView;


@end

@implementation PageView


- (instancetype)initWithFrame:(CGRect)frame withTitles:(NSArray *)titles withViewControllers:(NSArray *)controllers{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor greenColor];
        self.strongArray = [[NSMutableArray alloc] init];
        _titlesArray = titles;
        _controllersArray = controllers;
        [self createHeadView];
        [self createBtn];
        [self createScrollView];
        
    }
    return self;
}

- (void)createHeadView{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
    lineView.backgroundColor = [UIColor whiteColor];
    self.lineView = lineView;
    [self addSubview:lineView];
}

- (void)createBtn{
    if (_titlesArray.count != 0) {
        for (NSInteger i = 0; i < _titlesArray.count; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            CGFloat width = ([UIScreen mainScreen].bounds.size.width-5)/_titlesArray.count;
            button.backgroundColor = [UIColor whiteColor];
            button.frame = CGRectMake((width+5)*i, 0, width, 40);
            button.tag = 100+i;
            [button setTitle:_titlesArray[i] forState:(UIControlStateNormal)];
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(myClickBtn:) forControlEvents:(UIControlEventTouchUpInside)];
            [self.lineView addSubview:button];
            if (i == 0) {
                [button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
            }
            
        }
    }
}

- (void)createScrollView{
    [self addSubview:self.scrollView];
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.scrollsToTop = NO;
        _scrollView.frame = CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-40-44-40-83);
        _scrollView.delegate = self;
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * _titlesArray.count, 0);
        _scrollView.backgroundColor = [UIColor yellowColor];
        _scrollView.pagingEnabled = YES;
        //去掉垂直滚动条
        _scrollView.showsVerticalScrollIndicator = FALSE;
        //去掉横向滚动条
        _scrollView.showsHorizontalScrollIndicator = FALSE;
        for (NSInteger i = 0 ; i < _controllersArray.count; i++) {
            NSString *className = _controllersArray[i];
            Class class = NSClassFromString(className);
            UIViewController *viewController = class.new;
            [self.strongArray addObject:viewController];
            viewController.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-40-44-40-83);
            [_scrollView addSubview:viewController.view];
        }
    }
    
    return _scrollView;
}

- (void)myClickBtn:(UIButton *)mybutton{
    for (NSInteger i = 0 ; i < _titlesArray.count; i++) {
        UIButton *forButton = [self.lineView viewWithTag:100+i];
        if (mybutton.tag == forButton.tag) {
            [mybutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }else{
            [forButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    [self.scrollView setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width*(mybutton.tag-100), 0) animated:YES];
}







- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger x = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
    UIButton *selectBtn = [self.lineView viewWithTag:100+x];
    for (UIView *view in self.lineView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            if (view.tag == selectBtn.tag) {
                [selectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }else{
                UIButton *button = (UIButton *)view;
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
